package leetcode;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 寻找中位数
 * @author lbw
 */
public class FindMedianSortedArrays {
    public static void main(String[] args) {
        FindMedianSortedArrays f = new FindMedianSortedArrays();
        int[] nums1 = new int[]{1,3};
        int[] nums2 = new int[]{2};
        f.findMedianSortedArrays(nums1, nums2);
    }

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        List<Integer> list = new ArrayList<>();
        for (int i : nums1) {
            list.add(i);
        }
        for (int i : nums2) {
            list.add(i);
        }
        Collections.sort(list);
        if ((list.size() % 2) == 0){
            Integer one = list.get(list.size() / 2);
            Integer two = list.get((list.size() / 2) - 1);
            return Double.parseDouble(String.valueOf(one + two)) / 2d;
        } else {

            return Double.parseDouble(String.valueOf(list.get((list.size() -1 ) / 2)));
        }
    }
}
